<?php
/*--------------------------
	file:	cache.class	-	[developing...]
	intro:	令牌创建和验证类
--------------------------*/
if(!defined('MIDP')) {
	exit('Access Denied');
}

class CACHE{
	private $cache_dir;
	private $cache_ext='.cache.php';
	private $expireTime=180;   
	function __construct($cache_dirname=''){
		global $cacheDir;		
		$cache_dirname && $this->cache_dir = $cache_dirname;
		!$cache_dirname && $this->cache_dir = $cache_dirname = $cacheDir;
		
		if(!@is_dir($cache_dirname)){
			if(!@mkdir($cache_dirname,0777)){    
				$this->warn('缓存文件不存在而且不能创建,需要手动创建.'); 
				return false; 
			}    
		} 
	}
	/*function __destruct(){    
		echo 'Cache class bye.';    
	} */   
		 
	function warn($errorstring){    
		echo "<b><font color='red'>发生错误:<pre>".$errorstring."</pre></font></b>";    
	}    
		 
	function writeData($pageurl,$pagedata){    
		if(!$fso=fopen($this->cache_dir.$pageurl,'w')){    
			$this->warn('无法打开缓存文件.');//trigger_error    
			return false;    
		}    
		if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定    
			$this->warn('无法锁定缓存文件.');//trigger_error 
			return false;
		}    
		if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式 
			$this->warn('无法写入缓存文件.');//trigger_error
			return false;
		}    
		flock($fso,LOCK_UN);//释放锁定  
		fclose($fso);
		return true;
	}    
		 
	function readData($cacheFile){
		$cacheFile =  $this->cache_dir.$cacheFile;
		if(!file_exists($cacheFile)){
			$this->warn('无法读取缓存文件.');//trigger_error    
			return false;
		}    
		echo '读取缓存文件:'.$cacheFile;    
		//return unserialize(file_get_contents($cacheFile));    
		$fso = fopen($cacheFile, 'r');
		$data = fread($fso, filesize($cacheFile)); 
		fclose($fso);
		return $data;
	}
	
/*	function writeArray($cachename,$array){
		$data = "<?php\n";
		$data.= '$_NCACHE[\''.$cachename.'\'] = '.$this->arrayeval($array);
		$data.= "\n?>";
		$this->writeData($cachename.'.cache.php',$data);
	}*/
	
	function makeVariable($variname, $value){
		if(substr($value,0,5)!='Array'){
			return '$_CACHE[\''.$variname.'\'] = \''.$value."';";
		}else{
			return '$_CACHE[\''.$variname.'\'] = '.$value.";";
		}
	}
	
	function writeModel($cachename,$str){
		$data = "<?php\n";
		$data.= $str;
		$data.= "\n?>";
		return $this->writeData($cachename.$this->cache_ext,$data);		
	}
	function readCache($cacheName){
		return $this->readData($cacheName.$this->cache_ext);
	}
	
	function arrayEval($array, $level = 0) {

		if(!is_array($array)) {
			return "'".$array."'";
		}
	
		$space = '';
		for($i = 0; $i <= $level; $i++) {
			$space .= "\t";
		}
		$evaluate = "Array\n$space(\n";
		$comma = $space;
		if(is_array($array)) {
			foreach($array as $key => $val) {
				$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
				$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
				if(is_array($val)) {
					$evaluate .= "$comma$key => ".$this->arrayEval($val, $level + 1);
				} else {
					$evaluate .= "$comma$key => $val";
				}
				$comma = ",\n$space";
			}
		}
		$evaluate .= "\n$space)";
		return $evaluate;
	}
	//@param:$filepath,file's path info
	function checkCache($filepath){
		if(!file_exists($filepath)){
			return false;
		}else{
			if(filemtime($filepath)<(time()-$this->expireTime)){
				return false;
			}else{
				return true;
			}
		}
	}  
	//param:moudle name,in system use!
	function makeModel($model){
		global $db,$tablepre;
		switch($model){
			case 'levels':
				//$this->writeData("data.cache.php","1234");
				$query = $db->query("SELECT * FROM {$tablepre}levels");
				$get_data = $db->fetch_array($query);
				if(!empty($get_data)){
					$data_array_string = $this->arrayeval($get_data);
				}
				$data_string = $this->makeVariable('data_array_string');
				//$this->writeModel('levels',$data_string);
				break;
			case 'sets':
				$query = $db->query("SELECT * FROM {$tablepre}sets");
				while($get_data = $db->fetch_array($query)){
					$data_array[$get_data['name']] = $get_data['value'];
				}
				//$data = ;
				/*$data_array = array();
				foreach($get_data as $key=>$value){
					$data_array[$value['name']] = $value['value'];
				}*/
				$sets = $this->arrayEval($data_array);
				//exit($sets);
				$data_string = $this->makeVariable('sets', $sets);
				break;
			case '':
				break;
		}
		$this->writeModel($model,$data_string);
	}
}
?>